package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo04FlatMap {
  def main(args: Array[String]): Unit = {


    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo04FlatMap")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val lineRDD: RDD[String] = sc.parallelize(List[String](
      "java,scala,python"
      , "java,scala,python"
      , "hadoop,hive,hbase,spark,flink"
      , "hadoop,hive,hbase,spark,flink"
      , "hadoop,hive,hbase,spark,flink"))

    /**
     * flatMap：转换算子，类似Scala List中的flatMap方法，
     * 传入一条数据 返回多条数据
     * 需要接收一个函数f：String => 返回值类型通常为Array或者是集合类
     * flatMap会将最终传入的函数f的返回值进行展开（扁平化处理）
     */

    val flatMapRDD: RDD[String] = lineRDD.flatMap(line => line.split(","))
    flatMapRDD.foreach(println)

  }

}
